//
//  InstaEmailViewController.m
//  InstaEmail
//
//  Created by Ramesh on 7/30/12.
//  Copyright (c) 2012 Ramesh. All rights reserved.
//

#import "InstaEmailViewController.h"

@interface InstaEmailViewController ()

@end

@implementation InstaEmailViewController

@synthesize emailPicker=emailPicker_;
@synthesize notesField=notesField_;

-(IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}

/*
 Method coming from the Message Framework Delegate's Protocol.
 This is called when the default system email app is closed
 */
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error;
{
    [self dismissModalViewControllerAnimated:YES];
}

-(IBAction)sendButtonTapped:(id)sender {
    NSLog(@"Send Email Button Pressed");
    
    NSString *theMessage = [NSString stringWithFormat:@"%@ I'm %@ and feeling %@ about it.",
                            
     notesField_.text ? notesField_.text:@"",
    [activities_ objectAtIndex:[emailPicker_ selectedRowInComponent:0]],
    [feelings_ objectAtIndex:[emailPicker_ selectedRowInComponent:1]]];
    
    
    
    //Sending the Email Using the Messaging Framework
    if([MFMailComposeViewController canSendMail]){
        
        //Brings the IOS Default Mail sending app to the foreground
        MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc]init];
        
        mailController.mailComposeDelegate=self;
        [mailController setSubject:@"Hey Machan !"];
        [mailController setMessageBody:theMessage isHTML:NO];
        [self presentModalViewController:mailController animated:YES];
        [mailController release];
    }
    else{
        NSLog(@"@%",@"Sorry,you need to setup mail first");
    }
    
    
    NSLog(@"%@",theMessage);
    NSLog(@"Email Sent...");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    activities_ = [[NSArray alloc] initWithObjects:@"sleeping",@"eating",@"working",@"thinking",@"crying",
                   @"begging",@"leaving",@"shopping",@"licking",@"shitting",nil];
    
    feelings_ = [[NSArray alloc] initWithObjects:@"awesome",@"sad",@"happy",@"ambivalent",@"nauseous",@"psyched",@"confused",
                        @"hopeful",@"anxious", nil];
}

#pragma mark -
#pragma mark Picker Datasource Protocol Logic

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if(component ==0){
        return [activities_ count];
    }
    else{
        return [feelings_ count];
    }
}

#pragma mark-
#pragma mark Picker Delegate Protocol

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if(component ==0){
        return [activities_ objectAtIndex:row];
    }
    
    else{
        return [feelings_ objectAtIndex:row];
    }
    
    return nil;
}


-(void)dealloc {
    [notesField_ release];
    [emailPicker_ release];
    [activities_ release];
    [feelings_ release];
    [super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
